﻿using IService;
using Server;
using System;
using System.Linq;
using System.Net;

namespace HProseTest
{
    class Program
    {
        static void Main(string[] args)
        {
            StartServer();

            for (int i = 0; i < 1000; i++)
            {
                StartClient();
            }

            while(Console.ReadLine()!="exit")
            {

            }
        }
        static int id = 0;
        private async static void StartClient()
        {
            int myId = ++id;
            Hprose.RPC.Client client = new Hprose.RPC.Client("http://127.0.0.1:8888");
            IChatService chatService = client.UseService<IChatService>();
            string content = await chatService.Query(myId.ToString(), 3);
            Console.WriteLine(content);
        }
        private async static void StartClientWithToken()
        {
            int myId = ++id;
            Hprose.RPC.Client client = new Hprose.RPC.Client("http://127.0.0.1:8888");
            client.RequestHeaders["token"] = myId % 10;
            IChatService chatService = client
                .UseService<IChatService>();
            string content = await chatService.Query(myId.ToString(), 3);
            Console.WriteLine(content);
        }



        private static void StartServer()
        {
            HttpListener listener = new HttpListener();
            int port = 8888;
            string uri = $"http://127.0.0.1:{port}/";
            listener.Prefixes.Add(uri);
            listener.Start();
            Console.WriteLine($"Started service {uri}");
            Hprose.RPC.Service service = new Hprose.RPC.Service();
            service.AddMethods(typeof(ChatService).GetMethods().Select(x => x.Name).Distinct().ToArray(),new ChatService());
            service.Bind(listener);
        }
    }
}
